package cn.gdcp.contentprovidetest;

import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ContentResolverTestActivity extends AppCompatActivity {
     private List<Contact> contactList=new ArrayList<>();
     private ListView listView;
    ContactAdapter adapter;
    ContentResolver resolver;
    Button queryBtn;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_resolver_test);
        grantContact();
        initList();
        initAdapter();
        initListView();
        listView.setAdapter(adapter);
        initContentResolver();
        initQuery();
        queryBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                grantContact();
            }
        });
    }

    private void grantContact() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},1);
            }else {
            queryContact();
        }

    }

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode==1){
            if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                       queryContact();
            }else {
                Toast.makeText(this,"授权失败",Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void queryContact() {
        Cursor cursor=null;
        try{
         cursor=resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
        if (cursor!=null){
        while(cursor.moveToNext()){
            String name=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String number=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            Contact contact=new Contact(name,number);
            contactList.add(contact);
            adapter.notifyDataSetChanged();}
        }
  }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (cursor!=null)
                cursor.close();
        }
    }
    private void initQuery() {
        queryBtn=findViewById(R.id.query_button);
    }

    private void initContentResolver() {
          resolver=getContentResolver();
    }

    private void initList() {
        Contact contact1=new Contact("陆经东","110");
        contactList.add(contact1);

    }

    private void initListView() {
        listView=findViewById(R.id.listview_contact);
    }

    private void initAdapter() {
        adapter=new ContactAdapter(ContentResolverTestActivity.this,R.layout.contact_item,contactList);
    }
}
